class Game
{
    constructor(el){
        this.el = document.querySelector(el);
        this.map = new Map(this.el);
        this.map.create();
        this.food = new Food(this.el);
        this.food.create();
        this.snake = new Snake(this.el);
        this.snake.create();
        this.t=null;
    }

    start(){
        clearInterval(this.t);
        this.t = setInterval(()=>{
            this.snake.move();

            let snakeX = this.snake.snakeObj[0].x*this.snake.len;
            let snakeY = this.snake.snakeObj[0].y*this.snake.len;
            let foodX = this.food.foodX;
            let foodY = this.food.foodY;
            if(snakeX==foodX && snakeY==foodY){
                let last = this.snake.snakeObj[this.snake.snakeObj.length-1];
                // this.snake.snakeObj.push(last);     //对象类型传地址

                // console.log(last,this.snake.snakeObj);
                this.snake.snakeObj.push({
                    x: last.x, 
                    y : last.y, 
                    color: last.color, 
                    el:null
                })
                // console.log(this.snake.snakeObj);
                this.snake.create();
                this.food.move();
            }
        },500)

        document.onkeydown=(evt)=>{
            let e = evt || window.event;
            switch (e.keyCode) {
                case 38:
                    this.snake.direction = 'top'
                    break;
                case 40:
                    this.snake.direction = 'bottom'
                    break;
                case 37:
                    this.snake.direction = 'left'
                    break;
                case 39:
                    this.snake.direction = 'right'
                    break;
                default:
                    break;
            }
        }
    }
    
    stop(){
        clearInterval(this.t)
    }
}